package com.kbbywyclient.util;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;

import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by FHW
 * on 2017/3/27
 * Description: 验证格式工具类
 */
public class CheckFormat {
    /**
     * 判断手机格式是否正确
     *
     * @param mobiles
     * @return
     */
    public static boolean isMobile(String mobiles) {
        String telRegex = "[1][3578]\\d{9}";
        return mobiles.matches(telRegex);
    }

    /**
     * 验证密码 6到15位
     *
     * @param pwd
     * @return
     */
    public static boolean isPwd(String pwd) {
        String telRegex = "^[a-zA-Z0-9]{6,15}$";
        return pwd.matches(telRegex);
    }

    public static boolean isPwdFinish(String pwd) {
        String telRegex = "^[a-zA-Z0-9]{15,15}$";
        return pwd.matches(telRegex);
    }

    /**
     * 图片验证码 4位
     *
     * @param pwd
     * @return
     */
    public static boolean isPicCode(String pwd) {
        String telRegex = "^[a-zA-Z0-9]{4,4}$";
        return pwd.matches(telRegex);
    }


    /**
     * 手机验证码
     *
     * @param code
     * @return
     */
    public static boolean isCode(String code) {
        return code.length() >= 6;
    }

    /**
     * 验证字符串是否是email
     *
     * @param email
     * @return
     */
    public static boolean isEmail(String email) {
        String str = "^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$";
        Pattern p = Pattern.compile(str);
        Matcher m = p.matcher(email);
        return m.matches();
    }

    /**
     * 验证密码是否包含英文
     *
     * @param password
     * @return
     */
    public static boolean pwdIsCN(String password) {
        String str = "[\\u4e00-\\u9fa5]";
        Pattern p = Pattern.compile(str);
        Matcher m = p.matcher(password);
        return !m.matches();
    }

    /**
     * 验证输入是否为整数
     *
     * @param floor
     * @return
     */
    public static boolean numberInt(String floor) {
        String str = "^[1-9]\\d*";
        Pattern p = Pattern.compile(str);
        Matcher m = p.matcher(floor);
        return m.matches();
    }

    /**
     * 验证输入是否为QQ
     *
     * @param qq
     * @return
     */
    public static boolean isQQ(String qq) {
        String str = "^[1-9]\\d{4,10}$";
        Pattern p = Pattern.compile(str);
        Matcher m = p.matcher(qq);
        return m.matches();
    }

    /**
     * 验证输入是否为QQ
     *
     * @param year
     * @return
     */
    public static boolean checkYear(String year) {
        String str = "^\\d{4}$";
        Pattern p = Pattern.compile(str);
        Matcher m = p.matcher(year);
        return m.matches();
    }

    /**
     * 验证输入是否为身份证
     *
     * @param idcardno
     * @return
     */
    public static boolean checkIDCardNO(String idcardno) {
        String str = "^(\\d{15}$|^\\d{18}$|^\\d{17}(\\d|X|x))$";
        Pattern p = Pattern.compile(str);
        Matcher m = p.matcher(idcardno);
        return m.matches();
    }

    /**
     * 验证输入是否为微信号
     *
     * @param weChatId
     * @return
     */
    public static boolean checkWeChat(String weChatId) {
        String str = "^[a-zA-Z\\d_]{5,}$";
        Pattern p = Pattern.compile(str);
        Matcher m = p.matcher(weChatId);
        return m.matches();
    }

    public static boolean cheackStringisEmpty(String string) {
        return ((null == string) || (string.equals("")));
    }

    /**
     * 港澳来往内地通行证格式验证
     */
    public static boolean hkongMacaoValidate(String idCard) {
        String str = "^[HMhm]{1}([0-9]{10}|[0-9]{8})$";
        Pattern p = Pattern.compile(str);
        Matcher m = p.matcher(idCard);
        return m.matches();
    }

    /**
     * 台湾来往内地通行证格式验证
     */
    public static boolean taiwValidate(String idCard) {
        String a = "^[0-9]{8}$";
        String b = "^[0-9]{10}$";
        Pattern p1 = Pattern.compile(a);
        Pattern p2 = Pattern.compile(b);
        Matcher m1 = p1.matcher(idCard);
        Matcher m2 = p2.matcher(idCard);
        return (m1.matches() || m2.matches());
    }

    /**
     * 护照
     */
    public static boolean passportValidate(String idCard) {
        String a = "^[a-zA-Z0-9]{5,17}$";
        String b = "^[a-zA-Z]{5,17}$";
        Pattern p1 = Pattern.compile(a);
        Pattern p2 = Pattern.compile(b);
        Matcher m1 = p1.matcher(idCard);
        Matcher m2 = p2.matcher(idCard);
        return (m1.matches() || m2.matches());

    }

    /**
     * 判断 是否安装qq
     * @param context
     * @param packageName
     * @return
     */
    public static boolean checkApkExist(Context context, String packageName) {
        if (packageName == null || "".equals(packageName))
            return false;
        try {
            ApplicationInfo info = context.getPackageManager().getApplicationInfo(packageName,
                    PackageManager.GET_UNINSTALLED_PACKAGES);
            return true;
        } catch (PackageManager.NameNotFoundException e) {
            return false;
        }
    }


    public static boolean idCardValidate(String idCard) {
        if (idCard.length() == 15) {
            return isValidityBrithBy15IdCard(idCard);       //进行15位身份证的验证
        } else if (idCard.length() == 18) {
            if (isValidityBrithBy18IdCard(idCard) && isValidityBrithBy18IdCard(idCard)) {   //进行18位身份证的基本验证和第18位的验证
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    public static boolean isValidityBrithBy15IdCard(String idCard) {
        int year = Integer.parseInt(idCard.substring(6, 8));
        int month = Integer.parseInt(idCard.substring(8, 10));
        int day = Integer.parseInt(idCard.substring(10, 12));
        Date temp_date = new Date(year, month - 1, day);
        // 对于老身份证中的你年龄则不需考虑千年虫问题而使用getYear()方法
        if (temp_date.getYear() != year || temp_date.getMonth() != month - 1 || temp_date.getDate() != day) {
            return false;
        } else {
            return true;
        }
    }

    public static boolean isValidityBrithBy18IdCard(String idCard) {
        int year = Integer.parseInt(idCard.substring(6, 10));
        int month = Integer.parseInt(idCard.substring(10, 12));
        int day = Integer.parseInt(idCard.substring(12, 14));
        Date temp_date = new Date(year, month - 1, day);
        // 这里用getFullYear()获取年份，避免千年虫问题
        if (temp_date.getYear() != year || temp_date.getMonth() != month - 1 || temp_date.getDate() != day) {
            return false;
        } else {
            return true;
        }
    }

}
